<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" width="850px" append-to-body>
    <div class="form-box new_form_box">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="120px" class="demo-ruleForm">
        <!-- 类型  编号 手机号码 保管人员 备注 -->
        <!-- <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" >
            <el-option
              v-for="dict in type"
              :key="dict.value"
              :label="dict.name"
              :value="dict.name"
            />
          </el-select>
        </el-form-item> -->
        <div class="tip" v-if="form.deviceType==2&&!oldForm.tel">
          提示：智能卡位修改手机号，请仔细核对设备IMEI或手机卡ICCID，以免手机号不匹配造成后续使用问题。如果你填写的手机号已存在，将合并到已存在的卡位上。
        </div>
        <el-form-item :label="form.deviceType==2?'卡位编号(IMEI)':'卡位编号'" prop="num">
          <span v-if="form.deviceType==2">{{form.num}}</span>
          <el-input v-model="form.num" v-else placeholder="请输入编号" />
        </el-form-item>
        <el-form-item v-if="form.deviceType==2" label="手机卡ICCID" prop="iccid">
          <span v-if="form.deviceType==2">{{form.iccid}}</span>
        </el-form-item>
        <el-form-item label="手机号码" v-if="form.deviceType==2&&oldForm.tel&&oldForm.tel!='nil'">
          <span >{{oldForm.tel}}</span>
        </el-form-item>
        <el-form-item label="手机号码" prop="tel" v-else>
          <el-input v-model="form.tel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item v-if="!(form.deviceType==2&&!oldForm.tel)" label="运营商" prop="telType">
          <el-select v-model="form.telType" placeholder="请选择运营商">
            <el-option v-for="(dict,key) in yysList" :key="key" :label="dict.dictLabel" :value="dict.dictLabel" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="!(form.deviceType==2&&!oldForm.tel)" label="保管人员" prop="custodian">
          <el-input v-model="form.custodian" placeholder="请输入保管人员" />
        </el-form-item>
        <!-- <span v-if="billTypeConfig&&billTypeConfig.length">
          <span v-for="custom in billTypeConfig" :key="custom.id">
            <el-form-item :label="custom.name" :prop="custom.key" v-if="custom.isShow">
              <el-input v-model="form[custom.key]" :placeholder="`请填写${custom.name}`" />
            </el-form-item>
          </span>
        </span> -->
        <el-row v-if="!(form.deviceType==2&&!oldForm.tel)">
          <el-form-item label-width="50px" label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请填写备注" :rows="4" style="width:600px" />
          </el-form-item>
        </el-row>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import phoneApi from "@/api/phoneManage";
export default {
  name: "addFina",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value && value.length != 11) {
        callback(new Error("请输入11位手机号"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      title: "新增卡位",
      isEdit: false,
      billTypeList: [],
      billTypeConfig: null, //自定义字段配置
      uploadImgUrl: "",
      imgShowList: [],
      newImages: [],
      imgFileList: [],

      // 表单参数
      form: {
        type: "手机卡",
        num: "",
        tel: "",
        custodian: "",
        remark: "",
        status: 1,
        telType:'',
        iccid:''
      },
      rules: {
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        num: [{ required: true, message: "请输入编号", trigger: "change" }],
        tel: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
      },
      oldForm:null,
      yysList: [],
    };
  },
  created() {},
  methods: {
    // 打开弹窗
    open(flag, data) {
      this.form = {
        type: "手机卡",
        num: "",
        tel: "",
        custodian: "",
        remark: "",
        status: 1,
        telType:'',
        iccid:''
      }
      if (flag == "edit") {
        this.title = "编辑卡位";
        this.isEdit = true;
        this.handleUpdate(JSON.parse(JSON.stringify(data)));
      } else {
        this.isEdit = false;
      }
      this.dialogVisible = true;
      this.getYysList()
    },
    // 获取运营商列表
    getYysList() {
      this.getDicts("telType").then((r) => {
        if (r.data && r.data.length) {
         this.yysList = this.arrayToObject(r.data, "dictLabel");
        } 
      });
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    /** 查询详情进行回显 */
    handleUpdate(row) {
      this.form = row;
      this.oldForm = JSON.parse(JSON.stringify(row))
    },
    submitForm(formName) {
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            phoneApi.editPhone(this.form).then((res) => {
              this.$message.success(res.msg);
              this.reset();
              this.dialogVisible = false;
              this.$parent.getList();
            });
          } else {
            // 新增
            //console.log(this.form);
            phoneApi.addPhone(this.form).then((res) => {
              this.$message.success(res.msg);
              this.reset();
              this.dialogVisible = false;
              if (this.$parent.getList) {
                this.$parent.getList();
              } else {
                this.$emit("add");
              }
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // // 详情图片上传成功
    // handleUploadSuccess(res, file, fileList) {
    //   if (res.code == 200) {
    //     // 取得新上传的文件
    //     this.newImages = fileList.filter(t => {
    //       return t.url.indexOf("blob:") > -1;
    //     });
    //   }
    // },
    // // 详情图片删除
    // handleRemove(response, fileList, file) {
    //   const that = this;
    //   // 1.获取将要删除图片的临时路径
    //   const filePath = response.url;
    //   // 2.找到图片对应的索引值
    //   this.imgFileList.forEach(function(element, key) {
    //     if (element.url === filePath) {
    //       that.imgFileList.splice(key, 1); //回显的图片
    //     }
    //   });
    //   this.newImages.forEach(function(element, key) {
    //     if (element.url === filePath) {
    //       that.newImages.splice(key, 1); //新上传的图片
    //     }
    //   });
    // },
    // // 图片预览
    // handlePictureCardPreview(file) {
    //   this.$viewerApi({
    //     images: [file.url]
    //   });
    // },
    // uploadNumber(file, fileList) {
    //   //限制图片数量
    //   this.$modal.msgError("最多只能上传50张图片");
    // },
    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.tip{
  color: red;
  margin-bottom: 15px;
}
</style>
